<template>
	<view>
		<!--导航栏-->
		<pageLayout
			barTitle="字典数据"
			:isSearchNull="isSearchNull"
			:isOpen="isOpen"
			@rightClick="clickSearch">
		</pageLayout>
		<!--查询弹窗-->
		<up-popup :z-index="90" :show="isOpen" @close="closeSearch" mode="top">
			<scroll-view class="search" scroll-y="true" @touchmove.stop>
				<view class="search-form">
					<uni-forms ref="searchRef" :modelValue="searchModel" label-width="160rpx">
						<uni-forms-item label="字典标签">
							<uni-easyinput v-model="searchModel.dictLabel" placeholder="请输入字典标签" />
						</uni-forms-item>
						<uni-forms-item label="状态">
							<uni-data-select
								v-model="searchModel.status"
								placeholder="请选择状态"
								:localdata="systemDictStatusList"></uni-data-select>
						</uni-forms-item>
					</uni-forms>
					<view class="search-footer main-flex">
						<up-button
							class="search-footer-item"
							text="搜索"
							type="primary"
							@click="searchSubmit"
							icon="search"></up-button>
						<up-button
							class="search-footer-item"
							@click="resetSubmit"
							form-type="reset"
							text="重置"></up-button>
					</view>
				</view>
			</scroll-view>
		</up-popup>
		<view class="content">
			<!--操作-->
			<view class="operate main-flex">
				<view class="operate-item" v-if="checkPermi(['system:dict:add'])">
					<up-button
						class="operate-btn main-flex"
						size="mini"
						type="primary"
						@click="addFun()">
						<uni-icons class="operate-btn-icon" type="plusempty" size="28rpx" color="#ffffff"></uni-icons>
						<view class="operate-btn-text">新增</view>
					</up-button>
				</view>
			</view>
			<!--内容-->
			<view class="table">
				<ty-table
					:data="tableList"
					:columns="columns"
					:sortable="true"
					rowKey="dictCode"
					:cellClassName="() => 'table-cell'"
					:headerCellClassName="() => 'table-cell'"
					:rowClassName="() => 'table-row'"
					:isOperateBtn="(row) => {
						return checkPermi(['system:dict:edit', 'system:dict:remove'])
					}"
					stripe
					border>
					<template v-slot:left="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">字典编码：<text class="table-td-text">{{row.dictCode}}</text></view>
							<view class="table-td-row">字典标签：<text class="table-td-text">{{row.dictLabel}}</text></view>
							<view class="table-td-row">字典键值：<text class="table-td-text">{{row.dictValue}}</text></view>
						</view>
					</template>
					<template v-slot:right="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">字典排序：<text class="table-td-text">{{row.dictSort}}</text></view>
							<view class="table-td-row">
								状态：
								<uni-tag size="small" :type="row.status === 1 ? 'error' : 'success'" :text="arrText(systemDictStatusList, row.status)" />
							</view>
							<view class="table-td-row">备注：<text class="table-td-text">{{row.remark}}</text></view>
						</view>
					</template>
					<template v-slot:operate="{ row }">
						<view class="table-td-operate table-cell table-td-row main-flex">
							<view class="table-operate-btn main-flex" v-if="checkPermi(['zhouyz:dict:edit'])">
								<up-button
									size="mini"
									type="primary"
									@click.stop="editFun(row)">
									<uni-icons class="table-operate-btn-icon" type="compose" size="24rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">修改</view>
								</up-button>
							</view>
							<view class="table-operate-btn main-flex" v-if="checkPermi(['zhouyz:dict:remove'])">
								<up-button
									class="table-operate-btn main-flex"
									size="mini"
									type="error"
									@click.stop="deleteFun(row.dictCode)">
									<uni-icons class="table-operate-btn-icon" type="trash" size="20rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">删除</view>
								</up-button>
							</view>
						</view>
					</template>
					<template v-slot:empty>
						<view class="table-empty">
							<up-empty mode="data"/>
						</view>
					</template>
				</ty-table>
			</view>
			<!--分页-->
			<view class="pagination" v-show="total > 0">
				<ty-pagination
					:total="total"
					:pageSize="pageSize"
					:currentPage="pageNum"
					:pageSizes="[10, 20, 30, 50]"
					mode="simple"
					layout="first,prev,page,next,last"
					@sizeChange="sizeChange"
					@change="pageChange">
				</ty-pagination>
			</view>
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, onMounted, ref } from 'vue'
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import { listData, delData } from "@/api/system/dict/data"
import { isObjectEmptySimple, copyText, arrText } from '@/utils/ty.js';
import useUserStore from '@/store/modules/user'
import { systemDictStatusList } from '@/utils/config.js'

	const userStore = useUserStore()
	const dictType = ref('')
	onLoad((option) => {
		if (option.dictType) {
			dictType.value = option.dictType
		}
		uni.$on('refreshList', () => {
			getList()
		})
	})
	
	// 搜索相关
	const isSearchNull = ref(true)
	const isOpen =  ref(false)
	const clickSearch = (val) => {
		isOpen.value = val
	}
	const closeSearch = (val) => {
		isOpen.value = false
	}
	// 确认搜索
	const searchRef = ref(null)
	const searchModel = ref({
		dictLabel: '',
		status: ''
	})
	const searchSubmit = () => {
		pageNum.value = 1
		tableList.value = []
		isOpen.value = false
		searchFirst()
	}
	// 重置搜索
	const resetSubmit = () => {
		// searchRef.value.resetFields()
		searchModel.value = {
			dictLabel: '',
			status: ''
		}
		isSearchNull.value = true
		isOpen.value = false
		searchFirst()
	}
	
	
	// 数据查询
	const pageNum = ref(1)
	const pageSize = ref(10)
	const total = ref(0)
	const tableList = ref([])
	const isLoading = ref(false)
	
	// 表格头部
	const columns = ref([
		{ title: '字典信息', key: 'left' },
		{ title: '状态排序', key: 'right' }
	])
	
	// 从第一页查询
	const searchFirst = () => {
		pageNum.value = 1
		tableList.value = []
		isSearchNull.value = isObjectEmptySimple(searchModel.value)
		getList()
	}
	//页数切换
	const pageChange = (page) => {
		pageNum.value = page
		getList()
	}
	//每页条数切换
	const sizeChange = (size) => {
		pageSize.value = size
		getList()
	}
	
	const getList = () => {
		const params = {
			pageNum: pageNum.value,
			pageSize: pageSize.value
		}
		if (searchModel.value.dictLabel) {
			params.dictLabel = searchModel.value.dictLabel
		}
		if (searchModel.value.status || searchModel.value.status === 0) {
			params.status = searchModel.value.status
		}
		if (dictType.value) {
			params.dictType = dictType.value
		}
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		listData(params).then(res => {
			tableList.value = res.rows || []
			total.value = res.total
			isLoading.value = false
			uni.hideLoading()
    }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	// 新增
	const addFun = () => {
		uni.navigateTo({
			url: `/pages/system/dictDataEdit?dictType=${dictType.value}`
		})
	}
	// 修改
	const editFun = (row) => {
		uni.navigateTo({
			url: `/pages/system/dictDataEdit?dictCode=${row.dictCode}`
		})
	}
	//删除
	const deleteFun = (dictCode) => {
		uni.showModal({
			title: '删除',
			content: `是否确认删除编号为"${dictCode}"的数据项?`,
			success: function (res) {
				if (res.confirm) {
					deleteOne(dictCode)
				}
			}
		})
	}
	const deleteOne = (dictCode) => {
		delData(dictCode).then(res => {
			modal.showToast('删除成功')
			getList()
		}).catch((err) => {
			modal.showToast(err)
		})
	}
	
	onMounted(() => {
		searchFirst()
	})
</script>

<style scoped>
.form {
	background-color: #fff;
}
.record-type-0 {
	background-color: #e7faf0;
	border-color: #d0f5e0;
	color: #13ce66;
	padding: 2px 6px !important;
}
.record-type-1 {
	background-color: #ffeded;
	border-color: #ffdbdb;
	color: #ff4949;
	padding: 2px 6px !important;
}
</style>
